import Vue from 'vue';

let idCounter = 4;

const defaultList = [
  {
    value: '学习js',
    isDone: false,
    id: 0,
  },
  {
    value: '不学习js',
    isDone: true,
    id: 1,
  },
  {
    value: '不不学习js',
    isDone: true,
    id: 2,
  },
  {
    value: '不不不学习js',
    isDone: false,
    id: 3,
  },
];

export default new Vue({
  data () {
    return {
      list: defaultList,
      showingList: defaultList.concat(),
    };
  },
  // 实例初始化完成时
  created () {
    // 添加
    this.$on('添加', (value) => {
      this.list.push({
        value,
        isDone: false,
        id: idCounter++,
      });
      this.showingList = this.list.concat();
    });
    // 未完成
    this.$on('未完成', () => {
      this.showingList = this.list.filter(item => !item.isDone);
    });
    // 已完成
    this.$on('已完成', () => {
      this.showingList = this.list.filter(item => item.isDone);
    });
    // 全部
    this.$on('全部', () => {
      console.log(this.list);
      this.showingList = this.list.concat();
    });
    // 清除已完成
    this.$on('清除已完成', () => {
      this.list = this.list.filter(item => !item.isDone);
      this.showingList = this.list.concat();
    });
  },
});
